package com.cj.st.sevice.impl;

import com.cj.st.config.PropertiesConfig;
import com.cj.st.sevice.FileService;
import com.cj.st.util.GsonUtil;
import com.cj.st.util.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class FileServiceImpl implements FileService {

    @Autowired
    PropertiesConfig propertiesConfig;

    public ReturnT fileUpload(MultipartFile file, HttpServletRequest request) {
        if (!file.isEmpty()) {
            //原始文件名应该避免重复，转换存储，做映射
            String saveFileName = file.getOriginalFilename();
            log.info("上传文件：{}", saveFileName);
            File saveFile = new File(propertiesConfig.getJoinedDir() + saveFileName);
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            BufferedOutputStream out = null;
            try {
                out = new BufferedOutputStream(new FileOutputStream(saveFile));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return ReturnT.FAIL.setMsg("上传失败:" + saveFileName);
            } catch (IOException e) {
                return ReturnT.FAIL.setMsg("上传失败:" + saveFileName);
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return ReturnT.SUCCESS.setMsg(propertiesConfig.getReturnBaseUrl() + saveFileName);
        } else {
            return ReturnT.FAIL.setMsg("上传失败:文件为空");
        }
    }

    @Override
    @Transactional
    public ReturnT multiUploads(HttpServletRequest request) {
        List<String> strings = new ArrayList<>();
        boolean flag = true;
        List<MultipartFile> multipartFiles = ((MultipartHttpServletRequest) request).getFiles("file");
        if (multipartFiles == null || multipartFiles.size() == 0) {
            return ReturnT.FAIL.setMsg("文件为空");
        }
        for (int i = 0; i < multipartFiles.size(); i++) {
            MultipartFile file = multipartFiles.get(i);
            if (!file.isEmpty()) {
                ReturnT returnT = fileUpload(file, request);
                if (returnT.getCode() == 500) {
                    flag = false;
                }
                strings.add(returnT.getMsg());
            }
        }
        return flag ? ReturnT.SUCCESS.setMsg(GsonUtil.GsonString(strings)) : ReturnT.FAIL.setMsg(GsonUtil.GsonString(strings));
    }
}